package com.sunlands.deskmate.message.handler.sender.trigger;

import com.sunlands.deskmate.message.entity.Messages;
import com.sunlands.deskmate.message.handler.storager.unsync.UnsyncMessageStorager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * //TODO
 *
 * @author anjunling@sunlands.com
 */
@Slf4j
@Component("unsyncMessageAcquire")
public class UnsyncMessageAcquire {

    @Autowired
    private UnsyncMessageStorager storager;

//    @Autowired
//    private CardManager cardManager;
//
//    @Autowired
//    private UserClient userClient;

    public Messages getUnreadMessage(String sessionKey) {
        return storager.getUnreadMessages(sessionKey);
    }

    public List<Messages> getUnreadMessage(String sessionKey, Long min, Long max) {
        //获取未同步消息
        List<Messages> messages = storager.getUnreadMessages(sessionKey, min, max);

        log.debug("未读消息 messages = {}", messages);
        return messages;
    }

//    private OutboundMessage fromJson(String json) {
//        JSONObject jsonObject = JSONObject.parseObject(json);
//        OutboundMessage outboundMessage = JSON.toJavaObject(jsonObject, OutboundMessage.class);
//
//        String messageType = jsonObject.getString(MessageConstants.MSGTYPE);
//        MessageEnum typeEnum = MessageEnum.byType(messageType);
//        log.debug("messageType = {} typeEnum = {}", messageType, typeEnum);
//        if (Objects.isNull(typeEnum)) {
//            return null;
//        }
//        switch (typeEnum) {
//            case TEXT:
//                outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), TextMessage.class));
//                break;
//            case IMAGE:
//                outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), ImageMessage.class));
//                break;
//            case AUDIO:
//                outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), AudioMessage.class));
//                break;
//            case VIDEO:
//                outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), VideoMessage.class));
//                break;
//            case EVENT:
//                updateEventMessage(outboundMessage, jsonObject);
//                break;
//            case CARD:
//                updateCardMessage(outboundMessage, jsonObject);
//                break;
//            case RESOURCE_CARD:
//                outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), ResourcePackageMessage.class));
//                break;
//            case TIP:
//                updateTipMessage(outboundMessage, jsonObject);
//                break;
//            default:
//        }
//
//        return outboundMessage;
//    }


//    @Deprecated
//    private void messages(List<OutboundMessage> messages, JSONObject jsonObject) {
//        if (MessageEnum.TEXT.type.equalsIgnoreCase(jsonObject.getString(MessageConstants.MSGTYPE))) {
//            OutboundMessage outboundMessage = JSON.toJavaObject(jsonObject, OutboundMessage.class);
//            outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), TextMessage.class));
//            messages.add(outboundMessage);
//        } else if (MessageEnum.IMAGE.type.equalsIgnoreCase(jsonObject.getString(MessageConstants.MSGTYPE))) {
//            OutboundMessage outboundMessage = JSON.toJavaObject(jsonObject, OutboundMessage.class);
//            outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), ImageMessage.class));
//            messages.add(outboundMessage);
//        } else if (MessageEnum.AUDIO.type.equalsIgnoreCase(jsonObject.getString(MessageConstants.MSGTYPE))) {
//            OutboundMessage outboundMessage = JSON.toJavaObject(jsonObject, OutboundMessage.class);
//            outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), AudioMessage.class));
//            messages.add(outboundMessage);
//        } else if (MessageEnum.VIDEO.type.equalsIgnoreCase(jsonObject.getString(MessageConstants.MSGTYPE))) {
//            OutboundMessage outboundMessage = JSON.toJavaObject(jsonObject, OutboundMessage.class);
//            outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), VideoMessage.class));
//            messages.add(outboundMessage);
//        } else if (MessageEnum.EVENT.type.equalsIgnoreCase(jsonObject.getString(MessageConstants.MSGTYPE))) {
//            eventMessage(messages, jsonObject);
//        } else if (MessageEnum.CARD.type.equalsIgnoreCase(jsonObject.getString(MessageConstants.MSGTYPE))) {
//            cardMessage(messages, jsonObject);
//        } else if (MessageEnum.RESOURCE_CARD.type.equalsIgnoreCase(jsonObject.getString(MessageConstants.MSGTYPE))) {
//            OutboundMessage outboundMessage = JSON.toJavaObject(jsonObject, OutboundMessage.class);
//            outboundMessage.setContent(JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), ResourcePackageMessage.class));
//            messages.add(outboundMessage);
//        } else {
//            log.error("cannot parse {}", jsonObject.toJSONString());
//        }
//    }


//    private void updateEventMessage(OutboundMessage outboundMessage, JSONObject jsonObject) {
//        EventMessage eventMessage = JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), EventMessage.class);
//        if (eventMessage != null) {
//            if (EventMessage.ActionEnum.ENTER.getAction().equalsIgnoreCase(eventMessage.getAction())) {
//                eventMessage.setDesc(EventMessage.ActionEnum.ENTER.getDesc());
//            } else if (EventMessage.ActionEnum.LEAVE.getAction().equalsIgnoreCase(eventMessage.getAction())) {
//                eventMessage.setDesc(EventMessage.ActionEnum.LEAVE.getDesc());
//            }
//
//            outboundMessage.setContent(eventMessage);
//        }
//    }


//    private void updateTipMessage(OutboundMessage outboundMessage, JSONObject jsonObject) {
//        TipMessage tipMessage = JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), TipMessage.class);
//        if (tipMessage != null) {
//            if (TipMessage.ActionEnum.SESSION_START.getAction().equalsIgnoreCase(tipMessage.getAction())) {
//                tipMessage.setDesc(tipMessage.getDesc());
//            }
//            outboundMessage.setContent(tipMessage);
//        }
//    }

//    @Deprecated
//    private void eventMessage(List<OutboundMessage> messages, JSONObject jsonObject) {
//        OutboundMessage outboundMessage = JSON.toJavaObject(jsonObject, OutboundMessage.class);
//        //事件消息转换
//        EventMessage eventMessage = JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), EventMessage.class);
//        if (eventMessage != null) {
//            if (EventMessage.ActionEnum.ENTER.getAction().equalsIgnoreCase(eventMessage.getAction())) {
//                eventMessage.setDesc(EventMessage.ActionEnum.ENTER.getDesc());
//            } else if (EventMessage.ActionEnum.LEAVE.getAction().equalsIgnoreCase(eventMessage.getAction())) {
//                eventMessage.setDesc(EventMessage.ActionEnum.LEAVE.getDesc());
//            }
//
//            outboundMessage.setContent(eventMessage);
//            messages.add(outboundMessage);
//        }
//    }

//    private UserForm getUserForm(UserForm userForm) {
//        if(userForm == null){
//            return userForm;
//        }
//        String phoneNum = userForm.getPhoneNum();
//        if(StringUtils.isNotEmpty(phoneNum)){
//            if(phoneNum.length() >= 7){
//                phoneNum = phoneNum.substring(0,3) + "****" + phoneNum.substring(7);
//            }else if(phoneNum.length() < 7 && phoneNum.length() >= 4){
//                phoneNum = phoneNum.substring(0,3) + "****";
//            }
//            userForm.setPhoneNum(phoneNum);
//        }
//        String qq = userForm.getQq();
//        if(StringUtils.isNotEmpty(qq)){
//            if(qq.length() >= 7){
//                qq = qq.substring(0,3) + "****" + qq.substring(7);
//            }else if(qq.length() < 7 && qq.length() >= 4){
//                qq = qq.substring(0,3) + "****";
//            }
//            userForm.setQq(qq);
//        }
//        String wechat = userForm.getWechat();
//        if(StringUtils.isNotEmpty(wechat)){
//            if(wechat.length() >= 7){
//                wechat = wechat.substring(0,3) + "****" + wechat.substring(7);
//            }else if(wechat.length() < 7 && wechat.length() >= 4){
//                wechat = wechat.substring(0,3) + "****";
//            }
//            userForm.setWechat(wechat);
//        }
//        return userForm;
//    }

//    private void updateCardMessage(OutboundMessage outboundMessage, JSONObject jsonObject) {
//        CardMessage cardMessage = JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), CardMessage.class);
//        //卡片消息转换
//        CardInfo cardInfo = cardManager.convert(cardMessage.getCardId());
//        if (cardInfo != null) {
//            cardMessage.setTitle(cardInfo.getTitle());
//            cardMessage.setDesc(cardInfo.getDescText());
//            cardMessage.setImageUrl(cardInfo.getImage());
//            cardMessage.setUrl(cardInfo.getUrl());
//
//            outboundMessage.setContent(cardMessage);
//        } else {
//            log.error("{} cardInfo is null", cardMessage.getCardId());
//        }
//    }

//    @Deprecated
//    private void cardMessage(List<OutboundMessage> messages, JSONObject jsonObject) {
//        OutboundMessage outboundMessage = JSON.toJavaObject(jsonObject, OutboundMessage.class);
//        CardMessage cardMessage = JSON.toJavaObject(jsonObject.getJSONObject(MessageConstants.CONTENT), CardMessage.class);
//        //卡片消息转换
//        CardInfo cardInfo = cardManager.convert(cardMessage.getCardId());
//        if (cardInfo != null) {
//            cardMessage.setTitle(cardInfo.getTitle());
//            cardMessage.setDesc(cardInfo.getDescText());
//            cardMessage.setImageUrl(cardInfo.getImage());
//            cardMessage.setUrl(cardInfo.getUrl());
//
//            outboundMessage.setContent(cardMessage);
//            messages.add(outboundMessage);
//        } else {
//            log.error("{} cardInfo is null", cardMessage.getCardId());
//        }
//    }

}
